import qs from "qs"
import ManageConstant from "../consts/manageConstant";
import cookies from "js-cookie";

class FavoritesService {

  constructor(_this) {
    this._this = _this;
    this._axios = _this.axios.create({
      baseURL: ManageConstant.DOMAIN_URL,
      timeout: 5000,
      headers: {
        Token: cookies.get('Token')
      }
    });
  }

  searchFavorites(params, executeMethod){
    this._axios.get(ManageConstant.FAVORITES_SEARCH, {params}).then(res => {
      if (res.data.resultCode == 0) {
        executeMethod(this._this, res.data);
      }
    })
  }

  addFavorites(params){
    this._axios.post(ManageConstant.FAVORITES_ADD, params).then(res => {
      if (res.data.resultCode == 0) {
        this._this.$Message.success("新建收藏夹成功");
        this._this.searchFavorites(0,10);
      }
      else{
        this._this.$Message.error("添加失败");
      }
    })
  }

  editFavorites(params){
    this._axios.post(ManageConstant.FAVORTIES_EDIT, params).then(res => {
      if (res.data.resultCode == 0) {
        this._this.$Message.success("编辑收藏夹成功");
        this._this.searchFavorites(0,10);
      }
      else{
        this._this.$Message.error("编辑失败");
      }
    })
  }

  batchDeleteFavoritess(favoritesIds){
    this._axios.post(ManageConstant.FAVORITES_BATCH_DELETE, qs.stringify({
      favoritesIds
    }, {
      indices: false
    })).then(res => {
      if (res.data.resultCode == 0) {
        this._this.$Message.success("删除收藏夹成功");
        this._this.searchFavorites(0,10);
      }
      else{
        this._this.$Message.error("删除失败");
      }
    })
  }

}
export default FavoritesService;
